(function(){
	'use strict';
	angular.module('controllers.workOrderData.eventMgmt')
	.controller('EventTrendController',EventTrendController);
	/**
	 *衡量指标:
	 * h3001  : 已关闭事件所占比率
	 * h3002  : 规定时间内解决事件所占比率
	 * h3003  : 一线维护率
	 * h3004  : 一次性通过率
	 * h3005  : 事件单升级率
	 * h3006  : 处理正确率
	 * h3007  : 告警平均解决时间
	 * h3008  : 告警处理及时率
	 * h3009  : 告警单成功关闭比率
	 */
	function EventTrendController($scope,workOrderDataService,eventTrendFactory,PublicFn,CoreLoading,eventMgmtFactory,$timeout,COREDATA){
		$scope.flag = {
			state: true,
			show: 'view',
			arrow: false,
			showPie: false
		};
		$scope.initQuery = function(){
			workOrderDataService.queryMeasureIndex({code:'3'},function(res){
				$scope.measureIndexs = res.data;
				$scope.search();
			});
		};
		$scope.sendData = workOrderDataService.getSendData('3');
		$scope.sendData.measureIndexCode = 'h3001';
		//导出路径
		$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/eventTrend/export';

		var myEcharts = echarts.init(document.getElementById('doc'));
		$scope.search = function(){
			$scope.flag.state = !$scope.flag.state;
			var timeTitle,measureIndexName,option,smallName,code;
			if(!$scope.flag.state){
				$scope.send = workOrderDataService.fmtSendData($scope.sendData);
				code = $scope.send.measureIndexCode;
				timeTitle = PublicFn.fmtDateTitle($scope.send,'timeType');
				measureIndexName = workOrderDataService.getName(code,$scope.measureIndexs,'measureIndexCode','measureIndexName');
				eventTrendFactory.queryTrend($scope.send,function(res){
					if(res.state === '10000' && res.data.length >=1){
						myEcharts.clear();
						smallName = eventTrendFactory.getShortName($scope.send);
						$scope.send.title = timeTitle + smallName + measureIndexName + '趋势';
						$scope.items = workOrderDataService.getArrayData(res.data);
						$scope.arrs = res.data;
						//echarts图提示信息
						if($scope.send.measureIndexCode !== 'h3007'){
							option = workOrderDataService.getEchartsLineData($scope.items,res.data,$scope.send.title,'percentage');
							option.tooltip.formatter = workOrderDataService.getFormatterFn('%');
							option.yAxis[0] = workOrderDataService.formatterYAxis('','{value}%');
						}else {
							option = workOrderDataService.getEchartsLineData($scope.items,res.data,$scope.send.title,'filedNum');
							option.tooltip.formatter = workOrderDataService.getFormatterFn('小时');
							option.yAxis[0] = workOrderDataService.formatterYAxis('单位:(小时)','');
						}
						option.grid = {
							x2:148
						};
						option.dataZoom = {
							show: true,
							realtime: true
						};
						myEcharts.setOption(option);
						$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
						$scope.flag.show = 'view';
						$scope.flag.arrow = true;
					}else if(res.data.length === 0){
						workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
					}else {
						workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		};

		$scope.changeMeasureIndex = function(code){
			if(code === 'h3004' || code === 'h3005' || code === 'h3006'){
				$scope.sendData.changeTypeCode = '1';
			}else {
				$scope.sendData.changeTypeCode = '';
			}
		};

		//监控页面时间，如果时间出错，则查询按钮禁止
		$scope.$watch('myForm.$invalid', function(newValue) {
    	newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
		});

	}
	EventTrendController.$inject = ['$scope','workOrderDataService','eventTrendFactory','PublicFn','CoreLoading','eventMgmtFactory','$timeout','COREDATA'];
})();